﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerInventoryItemDao : IInventoryItemDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="inventoryItem">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(InventoryItem inventoryItem)
        {
            return new object[]  
            {
                 "@InventoryItemID" , inventoryItem.InventoryItemId
                ,"@InventoryItemCode" , inventoryItem.InventoryItemCode
                ,"@InventoryItemName" , inventoryItem.InventoryItemName
              //  ,"@CostMethod" , inventoryItem.CostMethod
                ,"@Unit" , inventoryItem.Unit
            //    ,"@CurrencyCode" , inventoryItem.CurrencyCode
                ,"@AccountCode" , inventoryItem.AccountCode
                ,"@isActive" , inventoryItem.IsActive
                ,"@UnitID" , inventoryItem.UnitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, InventoryItem> Make = reader =>
          new InventoryItem
          {
              InventoryItemId = reader["InventoryItemID"].AsId(),
              InventoryItemCode = reader["InventoryItemCode"].AsString(),
              InventoryItemName = reader["InventoryItemName"].AsString(),
              AccountCode = reader["AccountCode"].AsString(),
              Unit = reader["Unit"].AsString(),
           //   CurrencyCode = reader["CurrencyCode"].AsString(),
            //  CostMethod = reader["CostMethod"].AsInt(),
              IsActive = reader["IsActive"].AsBool(),
              UnitId = reader["UnitID"].AsLong()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<InventoryItem> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_InventoryItem";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_InventoryItem";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the inventoryItem.
        /// </summary>
        /// <param name="inventoryItem">The inventoryItem.</param>
        /// <returns></returns>
        public string InsertInventoryItem(InventoryItem inventoryItem)
        {
            const string sql = "uspInsert_InventoryItem";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(inventoryItem));
        }

        /// <summary>
        /// Updates the inventoryItem.
        /// </summary>
        /// <param name="inventoryItem">The inventoryItem.</param>
        /// <returns></returns>
        public string UpdateInventoryItem(InventoryItem inventoryItem)
        {
            const string sql = "uspUpdate_InventoryItem";
            return Db.Update(sql, CommandType.StoredProcedure, Take(inventoryItem));
        }

        /// <summary>
        /// Deletes the inventoryItem.
        /// </summary>
        /// <param name="inventoryItem">The inventoryItem.</param>
        /// <returns></returns>
        public string DeleteInventoryItem(InventoryItem inventoryItem)
        {
            const string sql = @"uspDelete_InventoryItem";
            object[] parms = { "@InventoryItemID", inventoryItem.InventoryItemId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public InventoryItem GetInventoryItem(int inventoryItemId)
        {
            const string sql = @"uspGet_InventoryItem_ByID";
            object[] parms = { "@InventoryItemID", inventoryItemId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<InventoryItem> GetInventoryItemByUnit(int unitId)
        {
            const string sql = @"uspGet_InventoryItem_ByUnit";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<InventoryItem> GetInventoryItemListByStock(int unitId, int itemStock, int refId, DateTime postDate) 
        {
            const string sql = @"uspGet_All_InventoryItemByStockID";
            object[] parms = { "@StockID", itemStock, "@RefID", refId, "@RefDate", postDate, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
